## ----------------------------------------------------------------------
## Travis CI build script for Greenplum Database Open Source Project.
## ----------------------------------------------------------------------

language: cpp
cache: ccache

git:
  submodules: false

addons:
    apt:
        config:
            retries: true
        sources: &common_sources
            - ubuntu-toolchain-r-test
        packages: &common_packages
            - gcc-8
            - libxml2
            - libxml2-dev
            - libevent-dev
            - libperl-dev
            - libuv1-dev
            - g++-8
            - python-dev
            - python-yaml
            - libapr1-dev
            - libzstd1
            - libzstd1-dev

matrix:
    include:
        # OS and Compiler variations
        # ----------------------------------------------------------------
        #
        # Ubuntu Bionic, gcc 8
        - os: linux
          dist: bionic
          compiler: gcc
          env:
              - T=debug C=""
              - OVERRIDE_CC="CC=gcc-8" OVERRIDE_CXX="CXX=g++-8"
          addons:
              apt:
                  sources:
                      - *common_sources
                  packages:
                      - *common_packages
        # Ubuntu Xenial, clang 7
        - os: linux
          dist: xenial
          compiler: clang
          env:
              - T=debug C=""
              - OVERRIDE_CC="CC=clang-7" OVERRIDE_CXX="CXX=clang++-7"
          addons:
              apt:
                  sources:
                      - *common_sources
                      - llvm-toolchain-xenial-7
                  packages:
                      - *common_packages
                      - clang-7
        # macOS, XCode11
        - os: osx
          compiler: clang
          osx_image: xcode11
          env: T=macos
          addons:
            homebrew:
              packages:
                - ccache

        - stage: check-format
          os: linux
          dist: focal
          addons:
            apt:
              packages:
                - clang-format-11
                - parallel
          before_install: ~
          install: ~
          before_script: ~
          after_script: ~
          script:
            - env CLANG_FORMAT=clang-format-11 src/tools/fmt gen
            - git diff --exit-code
            - env CLANG_FORMAT=clang-format-11 src/tools/fmt chk

stages:
  - check-format
  - test

## ----------------------------------------------------------------------
## Build tools
## ----------------------------------------------------------------------

python:
    - "2.7"

before_install:
    - eval "${OVERRIDE_CC}"
    - eval "${OVERRIDE_CXX}"
    # ccache 3.2 (from Ubuntu Xenial) has CCACHE_CPP2 default to off (this
    # setting defaults to on starting from ccache 3.3). That default leads to
    # unlegible compiler warning outputs because GCC and Clang will emit
    # warnings using the preprocessed output...
    - |
      if [ "${TRAVIS_DIST}" = xenial ]; then
        ccache --set-config run_second_cpp=true
      fi
    - |
      if [ "${TRAVIS_COMPILER}" = clang ]; then
        case "${TRAVIS_OS_NAME}" in
          linux)
            sudo ln -sv ../../bin/ccache /usr/lib/ccache/${CC}
            sudo ln -sv ../../bin/ccache /usr/lib/ccache/${CXX}
            ;;
          osx)
            PATH=/usr/local/opt/ccache/libexec:$PATH
            ;;
        esac
      fi

## ----------------------------------------------------------------------
## Install supporting Python modules
## ----------------------------------------------------------------------

install:
    - pip install --user --upgrade pip
    - pip install --user --pre psutil
    - pip install --user lockfile
    - pip install --user setuptools

## ----------------------------------------------------------------------
## Perform build:
## ----------------------------------------------------------------------

before_script:
    - ssh-keygen -t "rsa" -f ~/.ssh/id_rsa -N ""
    - ssh-keyscan $(hostname) >> ~/.ssh/known_hosts
    - cp ~/.ssh/{id_rsa.pub,authorized_keys}
    - ccache --zero-stats

script:
  - |
      set -eo pipefail
      if [ "$T" = "debug" ]; then
        ./configure \
            --prefix=${TRAVIS_BUILD_DIR}/gpsql \
            --enable-cassert \
            --enable-debug \
            --enable-debug-extensions \
            --with-perl \
            --with-python \
            --disable-orca \
            --with-openssl \
            --with-ldap \
            --with-libcurl \
            --with-libxml \
            --enable-mapreduce \
            --enable-orafce \
            --enable-ic-proxy \
            $C
        make -s install
        source ${TRAVIS_BUILD_DIR}/gpsql/greenplum_path.sh
        make -s unittest-check
        make -C gpAux/gpdemo cluster
        source gpAux/gpdemo/gpdemo-env.sh
        make -C src/test/regress installcheck-small
      fi
  - |
      set -eo pipefail
      if [ "$T" = "production" ]; then
        ./configure \
            --prefix=${TRAVIS_BUILD_DIR}/gpsql \
            --with-perl \
            --with-python \
            --disable-orca \
            --with-openssl \
            --with-ldap \
            --with-libcurl \
            --with-libxml \
            --enable-mapreduce \
            --enable-orafce \
            --enable-ic-proxy \
            $C
        make -s install
        source ${TRAVIS_BUILD_DIR}/gpsql/greenplum_path.sh
        make -s unittest-check
        make -C gpAux/gpdemo cluster
        source gpAux/gpdemo/gpdemo-env.sh
        make -C src/test/regress installcheck-small
      fi
  - |
      set -eo pipefail
      if [ "$T" = "macos" ]; then
        ./configure \
            --prefix=${TRAVIS_BUILD_DIR}/gpsql \
            --with-perl \
            --with-python \
            --disable-orca \
            --enable-orafce \
            --disable-gpfdist \
            --disable-pxf \
            --disable-gpcloud \
            --without-zstd \
            $C
        make -s install
        source ${TRAVIS_BUILD_DIR}/gpsql/greenplum_path.sh
        make -s unittest-check
      fi

after_script:
  - ccache --show-stats
  - source ${TRAVIS_BUILD_DIR}/gpsql/greenplum_path.sh
  - postgres --version
  - gpssh --version
